package org.flowable.pz.handler;

import lombok.extern.slf4j.Slf4j;
import org.flowable.common.engine.api.FlowableException;
import org.flowable.pz.entity.dto.FlowableResultDto;
import org.flowable.pz.exception.FlowableCustomException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import javax.validation.ConstraintViolationException;

@Slf4j
@RestControllerAdvice(basePackages = "org.flowable.pz.controller")
public class FlowableExceptionHandler {

    /**
     * Valid 校验异常
     */
    @ExceptionHandler(ConstraintViolationException.class)
    public FlowableResultDto<String> handleConstraintViolationException(ConstraintViolationException e){
        // 打印堆栈信息
        log.error("flowable 校验异常：{}", e.getMessage());
        return FlowableResultDto.error(e.getMessage());
    }

    /**
     * flowable 异常拦截
     * */
    @ExceptionHandler(FlowableCustomException.class)
    public FlowableResultDto<String> handlerFlowableCustomException(FlowableCustomException e){
        log.error("flowable 自定义异常：{}", e.getMessage());
        return FlowableResultDto.error(e.getMessage());
    }

    /**
     * flowable 抛出的异常
     * */
    @ExceptionHandler(FlowableException.class)
    public FlowableResultDto<String> handlerFlowableException(FlowableException e){
        //打印堆栈信息
        log.error("flowable 系统异常：{}", e.getMessage(), e);
        return FlowableResultDto.error(e.getMessage());
    }

    /**
     * flowable 模块统一异常处理
     * */
    @ExceptionHandler(Exception.class)
    public FlowableResultDto<String> handlerException(Exception e){
        log.error("flowable 统一异常：{}", e.getMessage(), e);
        return FlowableResultDto.error(e.getMessage());
    }

}
